﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Collections.ObjectModel;
using MvcApplication6.DAL.Helper;
using MvcApplication6.Models;

namespace MvcApplication6.DAL
{
    public class PagingDAL
    {

        //------------------Product search paging sale---------------------------------
        public List<ProductModel> ProductSearchSale(int pagendex, int flag, int pageSize)
        {
            List<ProductModel> lst = new List<ProductModel>();

            ProductModel obj = null;

            Collection<SqlParameter> parameterList = new Collection<SqlParameter>();

            parameterList.Add(new SqlParameter("@numberPage", pagendex));
            parameterList.Add(new SqlParameter("@flag", flag));
            parameterList.Add(new SqlParameter("@PageSize", pageSize));

            SqlDataReader dr = SQLHelper.ExecuteReader("spProductSearchSale", parameterList);

            while (dr.Read())
            {
                obj = new ProductModel();
                obj.Id = int.Parse(dr["id"].ToString());
                obj.title = dr["title"].ToString();
                obj.IdL1 = int.Parse(dr["id_l1"].ToString());
                obj.IdL2 = int.Parse(dr["id_l2"].ToString());
                obj.IdL3 = int.Parse(dr["id_l3"].ToString());
                obj.Position1 = int.Parse(dr["position1"].ToString());
                obj.Position2 = int.Parse(dr["position2"].ToString());
                obj.Position3 = int.Parse(dr["position3"].ToString());
                obj.Position4 = int.Parse(dr["position4"].ToString());
                obj.Position5 = int.Parse(dr["position5"].ToString());
                obj.Position6 = int.Parse(dr["position6"].ToString());
                obj.NewproFlag = int.Parse(dr["newpro_flag"].ToString());
                obj.ProsalesFlag = int.Parse(dr["prosales_flag"].ToString());
                obj.ProsaleFlag = int.Parse(dr["prosale_flag"].ToString());
                obj.Name = dr["name"].ToString();
                obj.Warranty = dr["warranty"].ToString();
                obj.MakeIn =  dr["make_in"].ToString();
                obj.Image = dr["image"].ToString();
                obj.Price = float.Parse(dr["price"].ToString());
                obj.Sale = int.Parse(dr["sale"].ToString());
                obj.InfPro = dr["inf_pro"].ToString();
                obj.IntPro = dr["int_pro"].ToString();
                obj.DelFlg = int.Parse(dr["del_flg"].ToString());

                lst.Add(obj);
            }
            dr.Close();
            return lst;
        }

        //------------------Product search paging sale---------------------------------
        public int ProductSearchSaleCount(int flag)
        {
            Collection<SqlParameter> parameterList = new Collection<SqlParameter>();

            parameterList.Add(new SqlParameter("@flag", flag));

            SqlDataReader dr = SQLHelper.ExecuteReader("spProductCountSale",parameterList);

            int sumRecord = 0;

            while (dr.Read())
            {

                sumRecord = int.Parse(dr["sumrecord"].ToString());
            }
            dr.Close();
            return sumRecord;
        }



        //------------------Product search paging sale---------------------------------
        public List<ProductModel> ProductPagingById(int PageIndex, int Id, int MenuLevel)
        {
            List<ProductModel> lst = new List<ProductModel>();

            ProductModel obj = null;

            Collection<SqlParameter> parameterList = new Collection<SqlParameter>();

            parameterList.Add(new SqlParameter("@numberPage", PageIndex));
            parameterList.Add(new SqlParameter("@MenuLevel", MenuLevel));
            parameterList.Add(new SqlParameter("@Id", Id));


            SqlDataReader dr = SQLHelper.ExecuteReader("spProductPagingById", parameterList);

            while (dr.Read())
            {
                obj = new ProductModel();
                obj.Id = int.Parse(dr["id"].ToString());
                obj.IdL1 = int.Parse(dr["id_l1"].ToString());
                obj.IdL2 = int.Parse(dr["id_l2"].ToString());
                obj.IdL3 = int.Parse(dr["id_l3"].ToString());
                obj.Position1 = int.Parse(dr["position1"].ToString());
                obj.Position2 = int.Parse(dr["position2"].ToString());
                obj.Position3 = int.Parse(dr["position3"].ToString());
                obj.ProsalesFlag = int.Parse(dr["prosales_flag"].ToString());
                obj.Name = dr["name"].ToString();
                obj.Warranty = dr["warranty"].ToString();
                obj.MakeIn = dr["make_in"].ToString();
                obj.Image = dr["image"].ToString();
                obj.Price = float.Parse(dr["price"].ToString());
                obj.Sale = int.Parse(dr["sale"].ToString());
                obj.InfPro = dr["inf_pro"].ToString();
                obj.IntPro = dr["int_pro"].ToString();
                obj.DelFlg = int.Parse(dr["del_flg"].ToString());

                lst.Add(obj);
            }
            dr.Close();
            return lst;
        }

        //------------------Product search paging sale---------------------------------
        public int ProductCountPagingById(int Id, int MenuLevel)
        {

            Collection<SqlParameter> parameterList = new Collection<SqlParameter>();

            parameterList.Add(new SqlParameter("@MenuLevel", MenuLevel));
            parameterList.Add(new SqlParameter("@Id", Id));

            SqlDataReader dr = SQLHelper.ExecuteReader("spProductCountPagingById", parameterList);

            int sumRecord = 0;

            while (dr.Read())
            {

                sumRecord = int.Parse(dr["sumrecord"].ToString());
            }
            dr.Close();
            return sumRecord;
        }


        //------------------Event search paging sale---------------------------------
        public List<EventModel> EventPagingById_ce(int Pagendex, int Id_ce)
        {
            List<EventModel> lst = new List<EventModel>();

            EventModel obj = null;

            Collection<SqlParameter> parameterList = new Collection<SqlParameter>();

            parameterList.Add(new SqlParameter("@numberPage", Pagendex));

            parameterList.Add(new SqlParameter("@id", Id_ce));

            SqlDataReader dr = SQLHelper.ExecuteReader("spEventPagingById_ce", parameterList);

            while (dr.Read())
            {
                obj = new EventModel();
                obj.Id = int.Parse(dr["id"].ToString());
                obj.Title = dr["title"].ToString();
                obj.Title2 = dr["title2"].ToString();
                obj.Image = dr["image"].ToString();


                lst.Add(obj);
            }
            dr.Close();
            return lst;
        }

        //------------------Product search paging sale---------------------------------
        public int EventCountPagingById_ce(int Id)
        {

            Collection<SqlParameter> parameterList = new Collection<SqlParameter>();

            parameterList.Add(new SqlParameter("@id_ce", Id));

            SqlDataReader dr = SQLHelper.ExecuteReader("spEventCountById_ce", parameterList);

            int sumRecord = 0;

            while (dr.Read())
            {

                sumRecord = int.Parse(dr["sumrecord"].ToString());
            }
            dr.Close();
            return sumRecord;
        }

        //------------------Product search by name---------------------------------
        public List<ProductModel> proDuctSearchByNme(string name, int pageIndex)
        {
            List<ProductModel> lst = new List<ProductModel>();
            Collection<SqlParameter> parameterList = new Collection<SqlParameter>();

            parameterList.Add(new SqlParameter("@NumberPage", pageIndex));
            parameterList.Add(new SqlParameter("@name", name));

            SqlDataReader dr = SQLHelper.ExecuteReader("spProductSearchByName", parameterList);
            ProductModel obj = null;
            while (dr.Read())
            {
                obj = new ProductModel();
                obj.Id = int.Parse(dr["id"].ToString());
                obj.IdL1 = int.Parse(dr["id_l1"].ToString());
                obj.IdL2 = int.Parse(dr["id_l2"].ToString());
                obj.IdL3 = int.Parse(dr["id_l3"].ToString());
                obj.Position1 = int.Parse(dr["position1"].ToString());
                obj.Position2 = int.Parse(dr["position2"].ToString());
                obj.Position3 = int.Parse(dr["position3"].ToString());
                obj.ProsalesFlag = int.Parse(dr["prosales_flag"].ToString());
                obj.Name = dr["name"].ToString();
                obj.Warranty = dr["warranty"].ToString();
                obj.MakeIn = dr["make_in"].ToString();
                obj.Image = dr["image"].ToString();
                obj.Price = float.Parse(dr["price"].ToString());
                obj.Sale = int.Parse(dr["sale"].ToString());
                obj.InfPro = dr["inf_pro"].ToString();
                obj.IntPro = dr["int_pro"].ToString();
                obj.DelFlg = int.Parse(dr["del_flg"].ToString());

                lst.Add(obj);
            }
            dr.Close();
            return lst;
        }

        //------------------Product search paging sale---------------------------------
        public int spProductCountSearchByName(string name)
        {

            Collection<SqlParameter> parameterList = new Collection<SqlParameter>();

            parameterList.Add(new SqlParameter("@name", name));

            SqlDataReader dr = SQLHelper.ExecuteReader("spProductCountSearchByName", parameterList);

            int sumRecord = 0;

            while (dr.Read())
            {

                sumRecord = int.Parse(dr["sumrecord"].ToString());
            }
            dr.Close();
            return sumRecord;
        }
    }
}